package dse.messages.singleversion;

import dse.*;
import dse.messages.*;

import java.util.*;

public class ReadRequestMessageHandler extends MessageSender implements
		MessageHandler {
	//private DataObject dobj;
	private CoordinatorSelector selector;
	//private ShareDownloader downloader;
	//private LockProcessor lockProcessor;
	//private LinkedList<Node> residentNodes;
	//private DataRequest dataRequest;
	//private int srf;
	//private Node coordinator;
	//private Node client;
	
	public ReadRequestMessageHandler(Experiment experiment, DataObject dobj, DataRequest req, LinkedList<Node> rns, CoordinatorSelector selector,
			ShareDownloader downloader, LockProcessor processor){
		super(experiment);
		//this.dobj = dobj;
		//this.dataRequest = req;
		//this.residentNodes = rns;
		//this.selector = selector;
		//this.lockProcessor = processor;
		//this.srf = 0;
		//this.coordinator = null;
	}
	
	@Override
	public boolean acceptMessage(Message message) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void messageInterupted(ExperimentRecorder recorder, Message message,
			double currentTime) {
		// TODO Auto-generated method stub
		recorder.debug(this, "msgIntrpt: " + message, currentTime);
		selector.currentCoordinatorFail(message.to(), recorder, currentTime);
	}

	@Override
	public void messageReceived(ExperimentRecorder recorder, Message message,
			double currentTime) {
		// TODO Auto-generated method stub
		//recorder.debug(this, "msgRcvd: " + message, currentTime);
		/*if (message.messageType() != MessageType.ReadRequest) {
			recorder.warn(
					"ReadRequestMessageHandler recieved " + 
					message.messageType() + " message."
					);
			return;
		}
		
		this.coordinator= this.dataRequest.getCoordinator();
		this.client = message.from();
		this.coordinator.setSequence(Math.max(coordinator.getSequence()+1, this.client.getSequence()));
		this.dataRequest.setTM((new Version(coordinator.getSequence(), coordinator.id())));
		if(this.coordinator.handleNewRequest(dobj.id(), dataRequest)){
			srf = dataRequest.getLockNumber();
			//coordinator.Lock(dobj.id(), this.dataRequest);
			coordinator.receiveReply(this.dataRequest, coordinator);
			coordinator.sequence();
			for(int i=1; i<srf; i++){
				Message m = super.createRequestLockMessage(coordinator, residentNodes.get(i), dobj, dataRequest, message.purpose(), this.selector, this.lockProcessor);
				recorder.debug(this, "new msg: " + m, currentTime);
				experiment.messageQueue().enqueueMessage(m, currentTime);
			}
		}
        else{
            //if(this.coordinator.id() == 566){
               //System.out.println("Node " + coordinator.id() + " Fail to lock data object " + dobj.id() + " put " + dataRequest.toString() + " queue");
               //System.out.println("     because reqest in tranx " + coordinator.getHasRequest(dobj.id()).getTranxID());
            //}
        }*/
	}
}
